import 'package:get/get.dart';
import 'package:tiktokadult/hj_model/video_model.dart';
import 'package:tiktokadult/hj_utils/api_service/tag_service.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class VideoTagLogic extends GetxController {
  final TagsBean? tagModel;
  final bool isShortStyle;

  VideoTagLogic(this.tagModel, {this.isShortStyle = false});

  int sortIndex = 0;
  final sorts = ['最多收藏', '最新上架', '最多观看'];
  List<int> params = [6, 2, 3]; //6: 最多收藏 2: 最新上架 3: 本月最热.
  int page = 1;
  bool isLoading = true;
  RefreshController? refreshController;
  List<VideoModel> dataSource = [];

  @override
  void onInit() {
    fetchPageData();
    super.onInit();
  }

  fetchPageData({bool isRefresh = true, bool showLoading = false}) async {
    if (isRefresh) {
      page = 1;
    }
    if (showLoading) {
      isLoading = true;
      update();
    }
    final res = await TagService.fetchTagList(tagModel?.id,
        page: page, size: 12, sortType: params[sortIndex], newsType: isShortStyle ? "SHORT" : "SP");
    if (isRefresh) {
      refreshController?.refreshCompleted();
      dataSource.clear();
    }
    if (res?.hasNext ?? false) {
      refreshController?.loadComplete();
    } else {
      refreshController?.loadNoData();
    }
    dataSource.addAll(res?.videos ?? []);
    page += 1;

    isLoading = false;
    update();
  }

  Future<TagsBean?> fetchTagDetail() async {
    final res = await TagService.fetchTagInfo(tagModel?.id ?? '');
    if (res != null) {
      tagModel?.name = res.name;
      update();
    }
  }
}
